﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data;
using BrightFrame;
using AIM.Model;

namespace AIM.DAL
{
    public class DepartmentHasArchiveTypeDAL
    {
        private DbProvider dbProvider = new DbProvider();

        public List<DepartmentHasArchiveType> QueryByDepartmentID(int departmentID)
        {
            List<DepartmentHasArchiveType> departmentHasArchiveTypeList = new List<DepartmentHasArchiveType>();
            using (IDataReader dr = dbProvider.ExecuteDataReader("SELECT DepartmentID,ArchiveTypeID FROM department_has_archive_type WHERE DepartmentID=?DepartmentID",
                dbProvider.CreateParameter("?DepartmentID", DbType.Int32, departmentID)))
            {
                while (dr.Read())
                {
                    departmentHasArchiveTypeList.Add(new DepartmentHasArchiveType
                    {
                        Department = new Department
                        {
                            DepartmentID = Convert.ToInt32(dr["DepartmentID"])
                        },
                        ArchiveType = new ArchiveType
                        {
                            ArchiveTypeID = Convert.ToInt32(dr["ArchiveTypeID"])
                        }
                    });
                }
            }
            return departmentHasArchiveTypeList;
        }
        /// <summary>
        /// 添加
        /// </summary>
        /// <param name="departmentHasArchiveType"></param>
        /// <returns></returns>
        public void Add(DepartmentHasArchiveType departmentHasArchiveType)
        {
            dbProvider.ExecuteNonQuery("INSERT INTO department_has_archive_type (DepartmentID,ArchiveTypeID) VALUES (?DepartmentID,?ArchiveTypeID)",
                dbProvider.CreateParameter("?DepartmentID", DbType.Int32, departmentHasArchiveType.Department.DepartmentID),
                dbProvider.CreateParameter("?ArchiveTypeID", DbType.Int32, departmentHasArchiveType.ArchiveType.ArchiveTypeID));
        }
        /// <summary>
        /// 根据部门删除所需要的档案类别
        /// </summary>
        /// <param name="departmentID"></param>
        public void DeleteByDepartmentID(int departmentID)
        {
            dbProvider.ExecuteNonQuery("DELETE FROM department_has_archive_type WHERE DepartmentID=?DepartmentID",
                dbProvider.CreateParameter("?DepartmentID", DbType.Int32, departmentID));
        }
    }
}
